package com.cskaoyan.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.stereotype.Component;

/**
 * @author stone
 * @date 2022/07/07 11:29
 */
@Component
public class CountTimeAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        // 委托类方法执行之前的时间
        long start = System.currentTimeMillis();
        // 委托类方法的执行 → 类似于method.invoke(instance,args)
        Object proceed = methodInvocation.proceed();
        // 委托类方法执行之后的时间
        long end = System.currentTimeMillis();
        // 时间的相减
        System.out.println(methodInvocation.getMethod().getName() +
                "方法的执行时间：" + (end - start));

        return proceed;
    }
}
